package cn.jbolt.ec.wx;

import cn.jbolt.common.model.BecMember;
import cn.jbolt.ec.member.MemberService;
import cn.jbolt.ec.order.MsgTempService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;

import java.text.SimpleDateFormat;
import java.util.Date;

public class SubscribeService {

    WxMpService wxMpService = GenService.getWxMpService("jmd");
    public static SubscribeService me = new SubscribeService();

    /**
     * 得到access_token
     * @return
     */
    public String getAccessToken(){
        try{
            return wxMpService.getAccessToken();
        }catch (Exception e){
            return null;
        }
    }

    /**
     * 无分销
     */
    public String  handle_no_para(String openid,String nickname,WxMpXmlMessage inMessage){

        String msg = MemberService.me.saveSubscribeMember(openid, nickname);

        MsgService.me.message_custom_send(openid);

        WxMpXmlOutMessage outMessage =MsgService.me.text_msg(msg, inMessage);
        if (outMessage == null) {
            return "";
        }
        return MsgService.me.text_msg(msg,inMessage).toXml();
    }

    /**
     * 未关注平台分销行为处理
     */

    public String handle_with_para_unconcerned(String openid, String nickname, WxMpXmlMessage inMessage, WxMpService wxMpService){
        String id_string = inMessage.getEventKey().substring(inMessage.getEventKey().indexOf("_") + 1);
        int id = Integer.parseInt(id_string);
        if (id == 0) {      //扫描的是平台二维码
            String msg = MemberService.me.saveSubscribeMember(openid, nickname);
            MsgService.me.message_custom_send(openid);
            WxMpXmlOutMessage outMessage = MsgService.me.text_msg(msg, inMessage);
            if (outMessage == null) {
                return "";
            }
            return outMessage.toXml();
        } else if (id != 0) {       //扫描的是分销二维码  id是spreader_id
            String msg = MemberService.me.saveSpreaderMember(openid, nickname, id);

            if(msg.equals("您已成功绑定分销商.")||msg.equals("欢迎来到嘉美达商城.")){
                BecMember becMember= new BecMember().dao().findFirst("SELECT nickname,spreader_id from bec_member where openid_jmd= ? ",openid);
                String keyword1=becMember.getNickname();
                BecMember becMemberSpreader=new BecMember().dao().findById(becMember.getSpreaderId());
                String spreaderOpenid= becMemberSpreader.getOpenidJmd();
                Date date = new Date();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String keyword2=dateFormat.format(date);
                MsgService.me.sendMsg(MsgTempService.spreader_success,spreaderOpenid,"祝贺您有新成员加入"
                        ,keyword1,keyword2,"","","","");
            }
            MsgService.me.message_custom_send(openid);
            WxMpXmlOutMessage outMessage =MsgService.me.text_msg(msg, inMessage);
            if (outMessage == null) {
                return "";
            }
            return outMessage.toXml();
        }
        return "";
    }

    /**
     * 已关注平台分销行为处理
     * @return
     */

    public String handle_with_para_concerned(WxMpXmlMessage inMessage,String openid,WxMpService wxMpService){
        Integer id = Integer.valueOf(inMessage.getEventKey());
        if (id == 0) {  //已关注扫描的是平台二维码
            WxMpXmlOutMessage outMessage = MsgService.me.text_msg("欢迎来到嘉美达商城", inMessage);
            if (outMessage == null) {
                return " ";
            }
            MsgService.me.message_custom_send(openid);
            return outMessage.toXml();
        }else if (id!=0){//已关注扫描的是分销二维码
            String msg= MemberService.me.saveAleaySpreaderMember(openid,id);
            if(msg.equals("您已成功绑定分销商")){
                BecMember becMember= new BecMember().dao().findFirst("SELECT nickname,spreader_id from bec_member where openid_jmd= ? ",openid);
                String keyword1=becMember.getNickname();
                BecMember becMemberSpreader=new BecMember().dao().findById(becMember.getSpreaderId());
                String spreaderOpenid= becMemberSpreader.getOpenidJmd();
                Date date = new Date();
                SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                String keyword2=dateFormat.format(date);

                MsgService.me.sendMsg(MsgTempService.spreader_success,spreaderOpenid,"祝贺您有新成员加入"
                        ,keyword1,keyword2,"","","","");
            }
            WxMpXmlOutMessage outMessage= MsgService.me.text_msg(msg,inMessage);
            if (outMessage==null){
                return " ";
            }
            MsgService.me.message_custom_send(openid);
            return outMessage.toXml();
        }
        return " ";
    }




}
